using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Weazel.Microblaze.Simulator.Components.OpbUartLite
{
  public partial class Rs232ConsoleControl : UserControl
  {
    private List<string> lines = new List<string>();
    private delegate void AppendCharacterDelegate(char c);

    private bool lastWasNewLine = false;

    public void AppendCharacter(char c)
    {
      this.Invoke(new AppendCharacterDelegate(appendCharacter), new object[] { c });     
    }

    private void appendCharacter(char c)
    {
      if (c == '\n' || c == '\r')
      {
        if(!lastWasNewLine)
          lines.Add("");

        lastWasNewLine = true;
      }
      else
      {
        lines[lines.Count - 1] += c;
        lastWasNewLine = false;
      }

      this.richTextBox1.Lines = lines.ToArray();
    }

    public Rs232ConsoleControl()
    {
      InitializeComponent();
      lines.Add("");
    }
  }
}
